package com.lizard.schedule.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.lizard.schedule.R;
import com.lizard.schedule.net.http.consts.AppType;
import com.lizard.schedule.net.http.consts.ReturnCode;
import com.lizard.schedule.net.http.request.HttpRequestManager;
import com.lizard.schedule.net.http.request.HttpResponseListener;
import com.lizard.schedule.net.http.request.bean.VersionRequest;
import com.lizard.schedule.net.http.response.bean.VersionResponse;
import com.lizard.schedule.persistence.sharedpreferences.CommonPref;
import com.lizard.schedule.service.UpgradeService;
import com.lizard.schedule.ui.view.ItemView;
import com.lizard.schedule.ui.activity.base.BaseActivity;
import com.lizard.schedule.ui.view.Titlebar;
import com.lizard.schedule.utils.ScheduleUtil;
import com.lizard.schedule.utils.StatusBarUtil;
import com.lizard.schedule.utils.TimeUtil;
import com.lizard.schedule.utils.ToastUtil;
import com.lizard.schedule.widget.ScheduleAlertDialog;
import com.lizard.schedule.widget.ScheduleProgressDialog;
import com.lizard.schedule.widget.ScheduleToast;

/**
 * 关于
 * <p/>
 * Created by wangyiqian on 15/12/16.
 */
public class AboutActivity extends BaseActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_about);

        // 设置状态栏颜色
        StatusBarUtil.setStatusBarColor(this, R.color.schedule_blue);

        initView();
    }

    private void initView() {
        initTitle();
        TextView curVersion = (TextView) findViewById(R.id.cur_version);
        curVersion.setText(getString(R.string.app_name) + " " + ScheduleUtil.getVersionName());
        ItemView checkVersion = (ItemView) findViewById(R.id.check_version);
        checkVersion.setOnClickListener(this);
    }

    /**
     * 标题栏UI初始化
     */
    private void initTitle() {
        Titlebar titlebar = (Titlebar) findViewById(R.id.title_bar);

        titlebar.setLeftBtnClk(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                back();
            }
        });
    }

    /**
     * 返回
     */
    private void back() {
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        back();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.check_version) {
            checkNewVersion();
        }
    }

    /**
     * 检查新版本
     */
    private void checkNewVersion() {
        final ScheduleProgressDialog.Builder progressDialogBuilder = new ScheduleProgressDialog.Builder(this);
        progressDialogBuilder.show();

        // 访问server获取最新版本信息
        VersionRequest request = new VersionRequest();
        HttpRequestManager.getInstance().request(request, new HttpResponseListener<VersionResponse>() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                progressDialogBuilder.cancel();
                ToastUtil.showScheduleToastS(R.string.network_error);
            }

            @Override
            public void onResponse(VersionResponse response) {
                progressDialogBuilder.cancel();
                if (ReturnCode.SUCCESS.equals(response.getReturnCode())) {
                    int lastVersionCode = response.getVersion();
                    String lastVersionName = response.getVersionName();
                    String versionInfo = response.getVersionInfo();
                    String appType = response.getAppType();
                    String versionLink = response.getVersionLink();

                    if (!AppType.ANDROID.equals(appType)) {
                        return;
                    }
                    
                    // test code
//                    lastVersionCode = 999;
//                    versionLink = "http://img.kuaidadi.com/download/kuaididache_4.4_20208_10001.apk";
//                    versionLink = "http://gdown.baidu.com/data/wisegame/b190053712ed65e5/kuaidedache_49.apk";
//                    versionLink = "http://dldir1.qq.com/diditaxi/apk/didi_psngr.apk";
//                    versionLink = "http://www.lizardx.com/schedule1.0.1-Schedule-release.apk";
//                    versionLink = "http://buildprod.xiaojukeji.com/download/one/finally/d/android/output/one_finally_apk/159/DiDi_one_finally_d_build_159.apk";

                    if (lastVersionCode > ScheduleUtil.getVersionCode()) { // 有新版本

                        alertUpdateNewVersion(lastVersionName, lastVersionCode, versionLink);

                    } else {
                        ToastUtil.showScheduleToastL(R.string.cur_version_is_newest);
                    }

                } else {
                    ToastUtil.showScheduleToastS(R.string.version_info_fetch_failed);

                }
            }
        });
    }

    /**
     * 提示用户并更新新版本
     *
     * @param newVersionName
     * @param newVersionCode
     * @param url
     */
    private void alertUpdateNewVersion(String newVersionName, final int newVersionCode, final String url) {

        String updateStringFormat = getString(R.string.new_version_alert_message);

        new ScheduleAlertDialog.Builder(this)
                .setMessage(String.format(updateStringFormat, newVersionName))
                .setPositiveButton(R.string.update, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        UpgradeService.start(AboutActivity.this, url, newVersionCode);
                    }
                })
                .setNegativeButton(R.string.cancel, null).show();
    }
}
